package com.masterclient.app.modules.income.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.income.dealer.model.DealerBankCardBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class DealerBankCardManagePresenter extends DealerBankCardManageContract.Presenter{

    @Override
    public void getBankCardInfo() {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().getBankCardList(), (Consumer<CommonBaseResponse<DealerBankCardBean>>) bean -> {
            if (bean.getCode() == 0){
                mView.getBankSuccess((List<DealerBankCardBean>) bean.getData());
            }else {
                mView.getBankCardFail(bean.getMsg());
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            mView.getBankCardFail("网络异常，请重试");
        }));
    }

    @Override
    public void bankRemove(String id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bankcard_cert_id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseResponse> observable = RetrofitUtil2.getInstance().bankCardRemove(body);
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse>) bean -> {
            if(bean.getCode() == 0){
                mView.BankRemoveSuccess();
            }else {
                mView.BankRemoveFailed(bean.getMsg());
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void updateBankCardDefault(String id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bankcard_cert_id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseResponse> observable = RetrofitUtil2.getInstance().updateBankCardDefault(body);
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse>) bean -> {
            if(bean.getCode() == 0){
                mView.updateBankCardDefaultSuccess();
            }else {
                mView.updateBankCardDefaultFailed(bean.getMsg());
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}